<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单 
      :rules="loginFormRules"   验证输入框
      :model="loginForm"  表单的数据 双向绑定获取
      ref="loginFormRef"  引用对象调用其中方法，登录重置事件
      -->
      <el-form label-width="0px" class="login_form" :model="loginForm" :rules="loginFormRules" ref="loginFormv">
        <!-- 框1 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-denglu" v-model="loginForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <!-- 框2 -->
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-mimadenglu" v-model="loginForm.password" type="password" placeholder="密码"> </el-input>
        </el-form-item>
        <!--  按钮-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login('loginFormv')">登录</el-button>
          <el-button type="info" @click="resetLogin('loginFormv')">重置</el-button></el-form-item
        >
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'login',
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        loginFormRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            {
              min: 5,
              max: 12,
              message: '长度在 5 到 12 个字符',
              trigger: 'blur'
            }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            {
              min: 5,
              max: 10,
              message: '长度在 5 到 10 个字符',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      resetLogin(x) {
        //(this)
        this.$refs[x].resetFields()
      },
      login(x) {
        this.$refs[x].validate(async (valid) => {
          valid
          if (!valid) return
          // 解构赋值
          const { data: res } = await this.$http.post('login', this.loginForm)
          //(res) //token 后端返回的结果中
          if (res.meta.status !== 200) return this.$message.error('用户名或密码错误')
          this.$message.success('登录成功')
          // 保存到sessionStorage 中
          window.sessionStorage.setItem('token', res.data.token)
          // 编程式导航 $router
          this.$router.push('/home')
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  .login-container {
    background-color: rgb(72, 74, 91);
    // 百分之百
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .login-box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: relative;

    // 语法嵌套
    .avatar_box {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      // padding: 10px;
      // box-shadow: 0 0 10px #ddd;
      background-color: rgb(247, 246, 246);
      // // 移动本身的一半
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        border-radius: 50%;
        width: 100%;
        height: 100%;
        background-color: #eee;
      }
    }
    .login_form {
      width: 100%;
      padding: 0px 20px;
      box-sizing: border-box;
      position: absolute;
      bottom: 0px;
    }
    .btns {
      display: flex;
      justify-content: center;
    }
  }
</style>
